import 'package:flutter/material.dart';
import 'package:sow_app/presenter/state_tag_novel_presenter.dart';
import 'package:sow_app/view/home/info/login/become_author_page.dart';
import 'package:sow_app/view/home/info/login/change_password_page.dart';
import 'package:sow_app/view/home/info/login/feedback_page.dart';
import 'package:sow_app/view/library/classify/tag_novel_page.dart';

import 'package:sow_app_core/sow_app_core.dart';
import 'package:sow_app_i18n/sow_app_i18n.dart';

import '../presenter/state_app_presenter.dart';
import '../presenter/state_chapter_detail_presenter.dart';
import '../presenter/state_classify_presenter.dart';
import '../presenter/state_feedback_presenter.dart';
import '../presenter/state_home_presenter.dart';
import '../presenter/state_info_presenter.dart';
import '../presenter/state_library_presenter.dart';
import '../presenter/state_login_presenter.dart';
import '../presenter/state_novel_detail_presenter.dart';
import '../presenter/state_register_presenter.dart';
import '../presenter/state_search_presenter.dart';
import '../presenter/state_setting_presenter.dart';
import 'common/novel/chapter_detail_page.dart';
import 'common/novel/novel_detail_page.dart';
import 'home/home_page.dart';
import 'home/info/info_page.dart';
import 'home/info/login/login_page.dart';
import 'home/info/login/register/register_page.dart';
import 'home/info/personal_info.dart';
import 'home/info/setting/setting_page.dart';
import 'library/classify/classify_page.dart';
import 'library/library_page.dart';
import 'library/search/search_page.dart';

class App extends StatefulWidget {
  const App(
      {super.key,
      required this.client,
      required this.controller,
      required this.presenter,
      required this.homeController,
      required this.libraryController,
      required this.novelDetailController,
      required this.chapterDetailController,
      required this.infoController,
      required this.classifyController,
      required this.tagNovelController,
      required this.searchController,
      required this.settingController,
      required this.loginController,
      required this.registerController,
      required this.homePresenter,
      required this.libraryPresenter,
      required this.novelDetailPresenter,
      required this.chapterDetailPresenter,
      required this.infoPresenter,
      required this.classifyPresenter,
      required this.tagNovelPresenter,
      required this.searchPresenter,
      required this.settingPresenter,
      required this.loginPresenter,
      required this.registerPresenter,
      required this.feedbackController,
      required this.feedbackPresenter});
  final RestClient client;
  final AppController controller;
  final HomeController homeController;
  final LibraryController libraryController;
  final NovelDetailController novelDetailController;
  final ChapterDetailController chapterDetailController;
  final InfoController infoController;
  final ClassifyController classifyController;
  final TagNovelController tagNovelController;
  final SearchController searchController;
  final SettingController settingController;
  final LoginController loginController;
  final RegisterController registerController;
  final FeedbackController feedbackController;

  final StateAppPresenter presenter;
  final StateHomePresenter homePresenter;
  final StateLibraryPresenter libraryPresenter;
  final StateNovelDetailPresenter novelDetailPresenter;
  final StateChapterDetailPresenter chapterDetailPresenter;
  final StateInfoPresenter infoPresenter;
  final StateClassifyPresenter classifyPresenter;
  final StateTagNovelPresenter tagNovelPresenter;
  final StateSearchPresenter searchPresenter;
  final StateSettingPresenter settingPresenter;
  final StateLoginPresenter loginPresenter;
  final StateRegisterPresenter registerPresenter;
  final StateFeedbackPresenter feedbackPresenter;

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  @override
  void initState() {
    widget.presenter.presenterContext = context;
    widget.presenter.presenterSetState = setState;
    widget.presenter.pages = [
      HomePage(
        controller: widget.homeController,
        presenter: widget.homePresenter,
      ),
      LibraryPage(
        key: GlobalKey(),
        controller: widget.libraryController,
        presenter: widget.libraryPresenter,
      ),
      InfoPage(
        controller: widget.infoController,
        presenter: widget.infoPresenter,
      )
    ];
    widget.controller.init();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.presenter.currentToken.isNotEmpty) {
      widget.homeController.init();
      widget.libraryController.init();
      widget.infoController.init();
    }
    return MaterialApp(
      title: I18n.demo_ebook,
      routes: {
        NovelDetailPage.routeName: (context) => NovelDetailPage(
              controller: widget.novelDetailController,
              presenter: widget.novelDetailPresenter,
            ),
        ChapterDetailPage.routeName: (context) => ChapterDetailPage(
              presenter: widget.chapterDetailPresenter,
              controller: widget.chapterDetailController,
            ),
        ClassifyPage.routeName: (context) => ClassifyPage(
              controller: widget.classifyController,
              presenter: widget.classifyPresenter,
            ),
        TagNovelListPage.routeName: (context) => TagNovelListPage(
              controller: widget.tagNovelController,
              presenter: widget.tagNovelPresenter,
            ),
        SearchPage.routeName: (context) => SearchPage(
              controller: widget.searchController,
              presenter: widget.searchPresenter,
            ),
        SettingPage.routeName: (context) => SettingPage(
              controller: widget.settingController,
              presenter: widget.settingPresenter,
            ),
        LoginPage.routeName: (context) => LoginPage(
              controller: widget.loginController,
              presenter: widget.loginPresenter,
            ),
        ChangePasswordPage.routeName: (context) => ChangePasswordPage(
              onChangePassword: widget.infoController.changePassword,
            ),
        PersonalInfoPage.routeName: (context) => PersonalInfoPage(
              user: widget.infoPresenter.user,
            ),
        BecomeAuthorPage.routeName: (context) => BecomeAuthorPage(
              onConfirm: () {},
            ),
        FeedbackPage.routeName: (context) => FeedbackPage(
              controller: widget.feedbackController,
              presenter: widget.feedbackPresenter,
            ),
        RegisterPage.routeName: (context) => RegisterPage(
              controller: widget.registerController,
              presenter: widget.registerPresenter,
            )
      },
      home: Builder(
        builder: (context) {
          return widget.presenter.currentToken.isEmpty
              ? Scaffold(
                  body: Center(
                    child: Column(
                      children: [
                        const Spacer(),
                        Expanded(
                          flex: 2,
                          child: Center(
                            child: Icon(
                              Icons.book,
                              color: Colors.grey.shade300,
                              size: 240,
                            ),
                          ),
                        ),
                        ElevatedButton(
                          onPressed: () async {
                            await Navigator.pushNamed(
                                context, LoginPage.routeName);
                            widget.controller.init();
                          },
                          child: const Text(
                            I18n.login,
                          ),
                        ),
                        const Spacer(
                          flex: 2,
                        )
                      ],
                    ),
                  ),
                )
              : Scaffold(
                  bottomNavigationBar: BottomNavigationBar(
                    currentIndex: widget.presenter.current,
                    onTap: widget.controller.pageChange,
                    items: const [
                      BottomNavigationBarItem(
                          icon: Icon(Icons.home), label: I18n.welcome),
                      BottomNavigationBarItem(
                          icon: Icon(
                            Icons.library_books,
                          ),
                          label: I18n.library),
                      BottomNavigationBarItem(
                          icon: Icon(
                            Icons.person,
                          ),
                          label: I18n.info),
                    ],
                  ),
                  body: IndexedStack(
                    index: widget.presenter.current,
                    children: widget.presenter.widgets,
                  ),
                );
        },
      ),
    );
  }
}
